import store from '@/store'
import { createRouter, createWebHistory } from 'vue-router'
// 懒加载
const Home = () => import('@/views/Home.vue')
const ItmeMusic = () => import('@/views/ItmeMusic.vue')
const Search = () => import('@/views/Search.vue')
const Login = () => import('@/views/Login.vue')
const InfoUser = () => import('@/views/InfoUser.vue')

// 引入 vuex 中的 index
const index = () => import('@/store/index.js')

const routes = [
  // 路由重定向
  { path: '', redirect: '/home' },
  // 首页
  { path: '/home', component: Home },
  // 歌单详情页面
  { path: '/itmeMusic', component: ItmeMusic },
  // 搜索
  { path: '/search', component: Search },
  // 登录
  { path: '/login', component: Login },
  // 个人信息
  {
    path: '/infoUser',
    //使用 路由守卫
    beforeEnter: (to, from, next) => {
      // 如果用户没有登录，就跳转到登录页面
      if (store.state.isLogin || store.state.token || localStorage.getItem('token')) {
        next()
      } else {
        next('/login')
      }
    },
    component: InfoUser,
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})

// 全局路由守卫，控制是否显示底部组件
router.beforeEach((to, from) => {
  if (to.path == '/login') {
    store.state.isFooterMusic = false
  } else {
    store.state.isFooterMusic = true
  }
})

export default router
